package org.nobject.common.swing.g;

import java.awt.Graphics;
import java.util.List;
import java.util.Vector;

import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

/**
 * GTable
 *
 * @author bianrongjun
 * @version 1.0
 */
public class GTable extends JTable implements IG{
	
	public static int CONFIGIDX_NAME=0;//列名
	public static int CONFIGIDX_TYPE=1;//类型
	public static int CONFIGIDX_WIDTH=2;//宽度
	public static int CONFIGIDX_EDITABLE=3;//是否能修改
	public static int CONFIGIDX_CELLEDITER=4;//修改器
	public static int CONFIGIDX_CELLRENDER=5;//渲染器
	public static int CONFIGIDX_VALUEHANDLER=6;//值处理
	public static int CONFIGIDX_FONTCOLORHANDLER=7;//字体颜色处理
	
	/**
	 * greateTableModel
	 */
	private GTableModel tm; 
	

	/** columnConfigs */
	private Object [][]columnConfigs;
	
	/**
	 * 构造函数
	 */
	public GTable(){
		super();
		this.core=new GComponent();
	}
	
	/**
	 * 构造函数 
	 * @param rowData
	 * @param configs
	 */
	public GTable(String name,List datas,Object [][] columnConfigs){
		this();
		Object[] columnNames=new Object[columnConfigs.length];
		Class[] columnClasses=new Class[columnConfigs.length];
		TableCellEditor [] columnCelleditors=new TableCellEditor [columnConfigs.length];
		TableCellRenderer [] tableCellRenderers=new TableCellRenderer[columnConfigs.length];
		for (int i = 0; i < columnConfigs.length; i++) {
			columnNames[i]=columnConfigs[i][CONFIGIDX_NAME];
			columnClasses[i]=(Class)columnConfigs[i][CONFIGIDX_TYPE];
			columnCelleditors[i]=(TableCellEditor)columnConfigs[i][CONFIGIDX_CELLEDITER];
			tableCellRenderers[i]=(TableCellRenderer)columnConfigs[i][CONFIGIDX_CELLRENDER];
		}
		
		this.tm=new GTableModel();
		this.tm.setDataVector(null, columnNames);
		this.tm.setColumnClass(columnClasses);
		this.setModel(this.tm);
		this.setName(name);
		this.columnConfigs=columnConfigs;
		//TableColumnModel columnModel;
		for (int i = 0; i < columnConfigs.length; i++) {
			this.getColumnModel().getColumn(i).setCellEditor(columnCelleditors[i]);
			if(tableCellRenderers[i]!=null) this.getColumnModel().getColumn(i).setCellRenderer(tableCellRenderers[i]);
		}
		
		for (int i = 0; i < columnConfigs.length; i++) {
			Object [] columnConfig=(Object[])columnConfigs[i];
			TableColumn tableColumn=this.getColumnModel().getColumn(i);
			tableColumn.setPreferredWidth((Integer)columnConfig[CONFIGIDX_WIDTH]);
		}
		//JTableHeader jth=this.getTableHeader();
		//jth.getColumnModel().getColumn(0).setCellRenderer();
		
		if(datas!=null)	setDates(datas);
		
	}
	
	
	/**
	 * 构造函数
	 * @param name
	 * @param rowData
	 * @param columnNames
	 * @param columnClasses
	 */
	public GTable(String name,Object [][] rowData,Object[] columnNames,Class[] columnClasses,TableCellEditor [] columnCelleditors){
		this();
		this.tm=new GTableModel();
		this.tm.setDataVector(rowData, columnNames);
		this.tm.setColumnClass(columnClasses);
		this.setModel(this.tm);
		this.setName(name);
		if(columnCelleditors!=null){
			for (int i = 0; i < columnCelleditors.length; i++) {
				if(columnCelleditors[i]!=null) this.getColumnModel().getColumn(i).setCellEditor(columnCelleditors[i]);
			}
		}
		
	}
	
	/** setDates */
	public void setDates(List datas){
		if(columnConfigs==null||datas==null) return;
		int columnCount=columnConfigs.length;
		for (int i = 0; i < datas.size(); i++) {
			Object object = (Object) datas.get(i);
			Object [] row=new Object[columnCount];
			for (int j = 0; j < columnCount; j++) {
				if(GHandler.class.isAssignableFrom(columnConfigs[j][CONFIGIDX_VALUEHANDLER].getClass()))
					row[j]=((GHandler)(columnConfigs[j][CONFIGIDX_VALUEHANDLER])).handle(object);
				else row[j]=object;
			}
			this.addRow(row);
		}
		
	}
	
	/**
	 * 设置数据
	 * @param rowData
	 * @param columnNames
	 */
	public void setData(Object [][] rowData,Object[] columnNames){
		((GTableModel)this.dataModel).setDataVector(rowData, columnNames);
	}
	
	/**
	 * 设置数据
	 * @param rowData
	 * @param columnNames
	 */
	public void setData(Vector rowData,Vector columnNames){
		this.tm.setDataVector(rowData, columnNames);
	}
	
	/**
	 * 设置数据
	 * @param rowData
	 */
	public void setData(Vector rowData){
		this.tm.setDataVector(rowData);
	}
	
	/**
	 * 添加行
	 * @param rowData
	 */
	public void addRow(Object []rowData){
		this.tm.addRow(rowData);
	}
	
	/**
	 * 添加行
	 * @param rowData
	 */
	public void addRow(Vector rowData){
		this.tm.addRow(rowData);
	}
	
	/**
	 * 添加行
	 * @param rowData
	 */
	public void addRow(int index,Object []rowData){
		this.tm.insertRow(index,rowData);
	}
	
	/**
	 * 添加行
	 * @param rowData
	 */
	public void addRow(int index,Vector rowData){
		this.tm.insertRow(index,rowData);
	}
	
	/**
	 * 删除行
	 * @param index
	 */
	public void removeRow(int index){
		this.tm.removeRow(index);
	}
	
	/* (non-Javadoc)
	 * @see java.awt.Container#removeAll()
	 */
	public void removeAll(){
		int c=this.tm.getRowCount();
		for (int i = c; i >0; i--) {
			this.tm.removeRow(i-1);
		}
	}
	
	/**
	 * 向上移动行
	 * @param startindex
	 */
	public void moveUpRow(int startindex,int endindex){
		int to=(startindex==0)?0:(startindex-1);
		this.tm.moveRow(startindex, endindex, to);
		this.setRowSelectionInterval(to, to+endindex-startindex);
	}
	
	/**
	 * 向下移动行
	 * @param index
	 */
	public void moveDownRow(int startindex,int endindex){
		int to=(endindex>=(tm.getRowCount()-1))?startindex:(startindex+1);
		this.tm.moveRow(startindex, endindex, to);
		this.setRowSelectionInterval(to, to+endindex-startindex);
	} 
	
	/**
	 * getData
	 * @return
	 */
	public Vector getData(){
		return this.tm.getDataVector();
	} 
	
	/** IG */
	private GComponent core;
	public GComponent getGCore() {return core;}
	
	
	public void paintChildren(Graphics g) {
		if(core!=null)core.paintChildren(g);
		super.paintChildren(g);
	}
}

